<script type="text/javascript">
//<{$lang.user}><{$lang.msg}>
var customer_id='';//<{$lang.customer_code}>
function getCustomer(id) {
    if (id) {
        $.ajax({
            type: "POST",
            async: false,
            dataType: "json",
            url: "/customer/balance/single",
            data:{'customer_id':id},
            success: function(json) {
                var msg = "";
                if (json.ask == 0) {
                    msg = "<{$lang.customer}><{$lang.not_exists}>!";
                } else {   //检查<{$lang.customer}>的<{$lang.status}>,并给出相应<{$lang.tip}>
                    var userStatus = json.result.customer_status;
                    var result = json.result;
                    switch (userStatus) {
                        case '0': msg = "<{$lang.customer}><{$lang.unactivate}>!";
                            break;
                        case '2': msg = "<{$lang.customer}><{$lang.stop}>!";
                            break;
                    }
                }
                if (msg != '') {
                    alertTip(msg);
                    $("#balanceForm").resetForm();//<{$lang.clear}>
                    $(".balanceLog").hide();
                    return;
                }
                $('#CustomerName').val(result.customer_first_name + " " + result.customer_last_name);
                $('#balance').val(result.cb_value);
                $('#company').val(result.customer_company_name);
                $("#currency").val(result.currency_id);
                $('#userCurrencyId').val(result.currency_id);
                $('#CustomerId').val(id);
                customer_id=id;//<{$lang.customer_code}>
                initData(0);//记录
                changeRate();
            }
        });
    } else {
        return;
    }
}
//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '<{$lang.ok}>': function() {
                $(this).dialog("close");
            }
        }
    });
}
$(function() {
    changeBaId($('#baId').val());//加载<{$lang.exchange_rate}>
    $("#balanceForm input").each(function() {
        $(this).addClass("text ui-widget-content");
    })
    <{if $lang.current_language=='cn'}>
    var dayNamesMin = ['日','一', '二', '三', '四', '五', '六'];
    var monthNamesShort = ['01月','02月','03月','04月','05月','06月','07月','08月','09月','10月','11月','12月'];

    $('.datepicker').datepicker({
        changeMonth: true,
        changeYear: true,
        dayNamesMin:dayNamesMin,
        monthNamesShort:monthNamesShort,
        dateFormat: 'yy-mm-dd'
    });
    <{else}>
    $('.datepicker').datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd'
    });
    <{/if}>
});


//改变<{$lang.currency}>的时候,<{$lang.edit}>[<{$lang.exchange_rate}>],[实际<{$lang.money}>].
function changeRate() {
    var rateArr = eval('(' + $('#jsonAllCurrency').val() + ')');		//将字符串转换成json数组
    var arriveCurrency = $('#arriveCurrency').val();

    if (parseInt(arriveCurrency) != arriveCurrency)                        //如果取回来的值不是整数,<{$lang.clear}><{$lang.exchange_rate}>,实际<{$lang.money}>
    {
        $('#rate').val('');
        $('#money').val('');
        $('#arriveCurrencyRate').val('');
        return;
    }
    var currency = $('#currency').val();
    var arriveCurrencyRate = rateArr[arriveCurrency] * 1;					//<{$lang.daozhan}><{$lang.currency}>对人民币<{$lang.exchange_rate}>
    var userCurrencyRate = rateArr[currency] * 1;							//<{$lang.customer}><{$lang.currency}>对人民币<{$lang.exchange_rate}>
    var rate = (arriveCurrencyRate / userCurrencyRate).toFixed(4);
    $('#rate').val(rate);												//<{$lang.edit}><{$lang.exchange_rate}>
    $('#arriveCurrencyRate').val(arriveCurrencyRate);

    //<{$lang.edit}>手续费率
    //if($('#baId').val() == '9')
    setFeeRate();
    setMoney();
}

//设置<{$lang.customer}><{$lang.useable}><{$lang.money}>
function setMoney() {
    var rate = $('#rate').val() * 1;										//<{$lang.daozhan}><{$lang.currency}>对<{$lang.customer}><{$lang.currency}>的<{$lang.exchange_rate}>
    var arriveMoney = $('#arriveMoney').val() * 1;							//<{$lang.daozhan}><{$lang.money}>
    var feeRate = ($('#baId').val() == '9' && document.getElementById('yes').checked == true) ? ($('#feeRate').val()) * 1 : 0;
    $('#money').val((arriveMoney * rate * (1 - feeRate)).toFixed(2));
}

//改变<{$lang.deposit}><{$lang.money}>的时候,如果<{$lang.exchange_rate}>不为<{$lang.null}>,<{$lang.edit}>[实际<{$lang.money}>]
function changeArriveMoney(arriveMoney) {
    var rate = $('#rate').val() * 1;
    if (0 != rate) {
        setMoney();
    }
}
//改变<{$lang.bank_account}>的时候,<{$lang.daozhan}><{$lang.currency}>的选项要根据银行来定,如果<{$lang.bank_account}>为paypal手工<{$lang.deposit}>,显示手续费
function changeBaId(baId) {
    $('#arriveCurrency > option').remove();
    $.ajax
            ({
                type:"GET",
                url:"/customer/balance/getaccountmap/ba_id/" + baId,
                dataType:"json",
                success:function(json) {
                    if(json.ask=='0')return;
                    var html = '';
                    $.each(
                            json.result,
                            function(key, item) {
                                html += '<OPTION VALUE="' + item.currency_id + '" ' + (item.currency_id == 6 ? " selected" : '') + ' >' + item.currency_cn_name + '</option>';
                            }
                            );
                    $('#arriveCurrency').append(html);
                    changeRate();
                    setFeeRate();
                }
            });

}

//根据<{$lang.bank_account}>和<{$lang.currency}>设置手续费率
function setFeeRate() {
    var baId = $('#baId').val();
    var arriveCurrency = $('#arriveCurrency').val();
    $.ajax
            ({
                type:"GET",
                url:"/customer/balance/getfeerate/ba_id/" + baId + "/currency_id/" + arriveCurrency,
                dataType:"json",
                success:function(json) {
                    if (json.result.bam_fee_rate > 0) {
                        $('#feeRate').val(json.result.bam_fee_rate);
                        $('#yes').attr('checked', true);
                        $('.fee').show();
                    } else {
                        $('#no').attr('checked', true);
                        $('#feeRate').val(0);
                        $('.fee').hide();
                    }
                }
            });
}

//更改<{$lang.operation}><{$lang.type}>的时候,页面可填写的控件要作相应隐藏<{$lang.or}>者显示.
function changeType() {
    var curType = $("[name=curType]:checked").val();
    if (curType == 0) {
        $('.fee1').hide();
        $('.fee').hide();
    }
    else {
        $('.fee1').show();
    }
}

//<{$lang.log}>
var paginationTotal = 10;//记录总数
function submitact() {
    var curType = $("[name=curType]:checked").val();
    var c_id = $("#CustomerId").val();
    if(c_id==''){
        alertTip('<span><img src="/images/no.gif"><{$lang.pls_input}><{$lang.customer_code}>!</span>');
        return;
    }
    if (curType == 0 ) {
        getCustomer(c_id);
    } else {
        postsubmit();
    }
}

function loadData(page, pageSize) {
    //var customer_id = $("#CustomerId").val();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/customer/balance/historylist/page/" + page + "/pageSize/" + pageSize,
        data: {'customer_id':customer_id},
        success: function(json) {
            var list = "";
            paginationTotal = json.count;
            if (json.ask == 0) {
                $(".balanceLog").hide();
            } else {
                $(".balanceLog").show();
                var i = page == 1 ? 1 : pageSize * (page - 1) + 1;
                $.each(json.result, function(key, val) {
                    if ((key + 1) % 2 == 1) {
                        list += "<tr class=''>";
                    } else {
                        list += "<tr class='even-tr'>";
                    }
                    var cbh_type = (val.cbh_type == 1) ? '<{$lang.deposit}>' : '<{$lang.payment}>';
                    var user_id = (val.user_id == 0) ? '<{$lang.system}>' : (val.user_id == -1) ? '<{$lang.customer}>' : val.user_id;
                    list += "<td>" + val.cbh_add_time + "</td>";
                    list += "<td>" + cbh_type + "</td>";
                    list += "<td>" + val.cbh_base_value + "</td>";
                    list += "<td>" + val.currency_rate + "</td>";
                    list += "<td>" + val.cbh_current_value + "</td>";
                    list += "<td class='note'>" + val.cbh_note + "</td>";
                    list += "<td>" + user_id + "</td>";
                    list += "</tr>";
                });
            }
            $("#loadListData").html(list);
        }
    });
}

//<{$lang.submit}><{$lang.deposit}><{$lang.data}>
function postsubmit() {
    vForm = "";
    vForm = $("#balanceForm").serializeArray();
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/customer/balance/recharge",
        data: vForm,
        success: function(json) {
            if (json.ask) {
                alertTip('<{$lang.deposit}><{$lang.success}>!');
                var CustomerId=$("#CustomerId").val();
                $("#balanceForm").resetForm();//<{$lang.clear}>
                getCustomer(CustomerId);//重新<{$lang.search}>
            } else {
                if (json.error == null)return;
                var html = '';
                $.each(json.error, function(key, val) {
                    html += '<span><img src="/images/no.gif">' + val + '</span>';
                })
                alertTip(html);
            }
        }
    });
}
//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '<{$lang.ok}>': function() {
                $(this).dialog("close");
            }
        }
    });
}
</script>
<style type="text/css">
    #iTitle div {
        display: inline-block;
        float: left;
        padding: 0 0 0 10px;
        margin-bottom: 12px;
    }

    .mySelect {
        height: 22px;
        border: 1px solid #CCCCCC;
    }

    #iTitle input {
        height: 20px;
        border: 1px solid #CCCCCC;
    }

    #content table {
        border: 0px solid #C9C9C9;
    }

    #content table tr {
        background: none repeat scroll 0 0 #F2F8FD;

    }

    #content table tr td {
        border: 1px solid #C9C9C9;
        padding: 3px 1px;
    }

    .note {
        word-break: break-all;
    }
</style>


<div id='content'>
    <form name="balanceForm" id="balanceForm">
        <table style="text-align:right" width="100%">
            <tr>
                <td id="dnidInfo" colspan="10" align="left"></td>
            </tr>

            <tr>
                <td style="width:10%"><{$lang.customer_code}>:</td>
                <td style="width:10%"><input type='text' name='CustomerId' id='CustomerId' align="left"
                                             onblur="getCustomer(this.value)" style="width:100%" value="">
                </td>
                <td style="width:10%"><{$lang.customer}><{$lang.username}>:</td>
                <td style="width:10%"><input type='text' name='CustomerName' id='CustomerName' align="left"
                                             style="background:#f2f8fd;width:100%" readonly>
                </td>

                <td style="width:10%"><{$lang.currency}>:</td>
                <td align="left" style="width:10%">&nbsp;&nbsp;
                    <SELECT NAME="currency" id="currency" SIZE="1" onchange="changeRate()" disabled>
                        <{section name="loop" loop=$currency}>
                        <option value="<{$currency[loop].currency_id}>"><{$currency[loop].currency_cn_name}></option>
                        <{/section}>
                    </select>
                </td>
                <td style="width:10%"><{$lang.current}><{$lang.balance}>:</td>
                <td style="width:10%"><input type='text' name='balance' id='balance' align="left"
                                             style="background:#f2f8fd;width:100%" readonly>
                <td style="width:20%" align="left">&nbsp;&nbsp;&nbsp;<{$lang.company}>:<input type='text' name='company' id='company'
                                                                               align="left"
                                                                               style="background:#f2f8fd;width:60%"
                                                                               size="10" readonly></td>
            </tr>

            <tr></tr>

            <tr>
                <td><{$lang.operation}><{$lang.type}>:</td>

                <td colspan="3" style="text-align:left">
                    <INPUT type='radio' name="curType" id="type1" value="1" CHECKED onclick="changeType()"><{$lang.deposit}>
                    <INPUT type='radio' name="curType" id="type0" value="0" onclick="changeType()"><{$lang.search}>
                </td>
                <td colspan="6"></td>
            </tr>

            <tr class="fee1"></tr>

            <tr class="fee1" style="text-align:center">
                <td><{$lang.daozhan}><{$lang.date}></td>
                <td colspan="3"><{$lang.bank_account}></td>
                <td><{$lang.currency}></td>
                <td><{$lang.money}></td>
                <td><{$lang.exchange_rate}></td>
                <td><{$lang.customer}><{$lang.useable}><{$lang.money}></td>
                <td><{$lang.transaction_code}></td>

            </tr>
            <tr></tr>

            <tr class="fee1" style="text-align:left">
                <td><input type="text" size="13" id="arriveTime" name="arriveTime" readonly class="datepicker"/></td>

                <td align="left" colspan="3">&nbsp;<SELECT NAME="baId" id="baId" SIZE="1"
                                                           onchange="changeBaId(this.value)">
                    <{section name="loop" loop=$bank}>
                    <option value="<{$bank[loop].ba_id}>"><{$bank[loop].ba_title}></option>
                    <{/section}>
                </SELECT>
                </td>

                <td name="arriveMoneyTd" id="arriveMoneyTd" align="left">&nbsp;&nbsp;
                    <SELECT NAME="arriveCurrency" id="arriveCurrency" SIZE="1" onchange="changeRate()">
                        <{section name="loop" loop=$currency}>
                        <option value="<{$currency[loop].currency_code}>"><{$currency[loop].currency_cn_name}></option>
                        <{/section}>
                    </SELECT>

                </td>

                <td><input type='text' name='arriveMoney' id='arriveMoney' align="right"
                           onblur="changeArriveMoney(this.value)" style="width:100%;text-align:right;padding-right:2px"
                           value="">
                </td>

                <td><input type='text' name="rate" value="1" onchange="setMoney()"
                           style="width:100%;text-align:right;padding-right:2px" id="rate"/>
                </td>

                <td><input type='text' name="money" value="" id="money" readonly
                           style="background:#f2f8fd;width:100%;text-align:right;padding-right:2px" readonly></td>

                <td><input type='text' name='udoId' id='udoId' align="left" style="width:80%" value="">

                </td>
            </tr>

            <tr class="fee"></tr>
            <tr class="fee" style="display:none">
                <td><{$lang.procedure}><{$lang.cost}>?:</td>
                <td style="text-align:left">
                    <INPUT type='radio' name="doFee" id="yes" value="1" CHECKED onclick="setMoney()"><{$lang.yes}>
                    <INPUT type='radio' name="doFee" id="no" value="0" onclick="setMoney()"><{$lang.no}>
                </td>

                <td><{$lang.procedure}><{$lang.rate}>:</td>
                <td><input type='text' name='feeRate' id='feeRate' align="left" style="background:#f2f8fd;width:100%"
                           readonly></td>
                <td colspan="6"></td>
            </tr>
            <tr class="fee1" style="display:none"></tr>
            <tr class="fee1">
                <td rowspan="2">
                    <{$lang.remark}>:
                </td>

                <td rowspan="2" colspan="8" style="text-align:left">
                    <textarea style="width:80%" name="reason" id="reason"></textarea>
                </td>
            </tr>
            <tr></tr>
            <tr>
                <td align="center" colspan="9">
                    <input type="button" value='<{$lang.submit}>' onclick="submitact();" class='addBtn'
                           style="background:#F0FFFF;width:60px">
                </td>
                <input type='hidden' value='<{$jsonAllCurrency}>' name="jsonAllCurrency" id="jsonAllCurrency">
                <input type='hidden' value='' name="arriveCurrencyRate" id="arriveCurrencyRate">
                <input type='hidden' value='' name="type" id="type">
                <input type='hidden' value='0' name="dn_id">
                <input type='hidden' value='' name="userCurrencyId" id="userCurrencyId">
            </tr>
        </table>
    </form>
</div>
<div class='balanceLog' style="display:none;">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="myTab">
        <tbody>
        <tr class="even-tr">
            <th width="15%"><{$lang.operation}><{$lang.time}></th>
            <th width="5%"><{$lang.type}></th>
            <th><{$lang.money}></th>
            <th><{$lang.exchange_rate}></th>
            <th><{$lang.balance}></th>
            <th width="22%"><{$lang.remark}></th>
            <th width="10%"><{$lang.op_user}></th>
        </tr>
        </tbody>
        <tbody id="loadListData"></tbody>
    </table>
    <div class="pagination"></div>
</div>